package com.starsea.book.ui.newpage

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityFeedBackReplyBinding
import com.starsea.book.ui.newpage.adapter.ImageShowAdapter
import com.starsea.book.ui.newpage.utils.loadRoundImage
import com.starsea.book.ui.newpage.viewmodel.UserViewModel
import com.starsea.book.utils.gone
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @createTime 2024-01-10 10 GMT+8
 * @desc : 反馈详情
 */
class FeedBackDetailsActivity : VMBaseActivity<ActivityFeedBackReplyBinding, UserViewModel>() {
    override val binding by viewBinding(ActivityFeedBackReplyBinding::inflate)
    override val viewModel by viewModels<UserViewModel>()

    var provideId = ""

    lateinit var mAdapter: ImageShowAdapter

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        setTitle(R.string.title_mine_feed_back)

        provideId = intent.getStringExtra("provideId") ?: ""

        viewModel.getFeedBackDetails(provideId)
        viewModel.getUserProfile()

        mAdapter = ImageShowAdapter(this) { value, position, view ->
            XPopup.Builder(this)
                .asImageViewer(
                    view,
                    value,
                    SmartGlideImageLoader()
                ).show()
        }

        binding.rvImg.layoutManager = GridLayoutManager(this, 3)

        binding.rvImg.adapter = mAdapter

        binding.rtvCommit.setOnClickListener {

            finish()
        }


        viewModel.userProfileResult.observe(this) {
            if (it.status == 200) {

                binding.ivHeader.loadRoundImage(this, it.data!!.headUrl)
                binding.tvNickName.text = it.data!!.custName

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@FeedBackDetailsActivity)
            } else {
                toastOnUi(it.msg)
            }
        }
        viewModel.feedBackDetailsResult.observe(this) {
            if (it.status == 200) {

                binding.tvContent.text = it.data!!.content

                val imgs = it.data!!.imgs

                if (!TextUtils.isEmpty(imgs)) {
                    val imgsList = imgs?.split(",")

                    if (imgsList != null && !imgsList.isNullOrEmpty()) {
                        binding.rvImg.visible()
                        mAdapter.setItems(imgsList)
                    } else {
                        binding.rvImg.gone()
                    }
                } else {
                    binding.rvImg.gone()
                }

                if (it.data!!.replyStatus == 1) {
                    binding.tvReplyContent.visible()
                    binding.tvReplyTime.visible()

                    binding.tvReplyContent.text = it.data!!.reply
                    binding.tvReplyTime.text = it.data!!.replyTime

                } else {
                    binding.tvReplyContent.gone()
                    binding.tvReplyTime.gone()
                }

            } else if (it.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this@FeedBackDetailsActivity)
            } else {
                toastOnUi(it.msg)
            }
        }


    }

    override fun setTitle(resId: Int) {
        super.setTitle(resId)
        binding.titleBar.setTitle(resId)
    }

}